﻿namespace TfsCloudDeploy.Workflow.Core.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Activities.Hosting;
    using System.Activities;

    /// <summary>
    /// Reflected from: Microsoft.TeamFoundation.Build.Workflow.Activities.ScheduleActionExtension.
    /// Assembly: Microsoft.TeamFoundation.Build.Workflow, Version=10.0.0.0.
    /// </summary>
    internal sealed class ScheduleActionExtension : IWorkflowInstanceExtension
    {
        // Fields
        private TimeSpan m_waitTimeout = TimeSpan.FromMinutes(5.0);
        internal static object ShutdownEvent = new object();

        // Methods
        private void OnBookmarkResumed(IAsyncResult result)
        {
            try
            {
                this.Owner.EndResumeBookmark(result);
            }
            catch (Exception)
            {
            }
        }

        public void ScheduleAction<T>(string bookmarkName, T item)
        {
            this.Owner.BeginResumeBookmark(new Bookmark(bookmarkName), item, this.m_waitTimeout, new AsyncCallback(this.OnBookmarkResumed), null);
        }

        public void ScheduleAction<T1, T2>(string bookmarkName, T1 item1, T2 item2)
        {
            this.Owner.BeginResumeBookmark(new Bookmark(bookmarkName), new Tuple<T1, T2>(item1, item2), this.m_waitTimeout, new AsyncCallback(this.OnBookmarkResumed), null);
        }

        public void Shutdown(string bookmarkName)
        {
            this.Owner.BeginResumeBookmark(new Bookmark(bookmarkName), ShutdownEvent, TimeSpan.MaxValue, new AsyncCallback(this.OnBookmarkResumed), null);
        }

        IEnumerable<object> IWorkflowInstanceExtension.GetAdditionalExtensions()
        {
            return null;
        }

        void IWorkflowInstanceExtension.SetInstance(WorkflowInstanceProxy instance)
        {
            this.Owner = instance;
        }

        // Properties
        private WorkflowInstanceProxy Owner { get; set; }
    }
}
